#include <vector>
#include <iostream>

using namespace std;
class Solution
{
public:
	Solution() : arr(256,0){}
  //Insert one char from stringstream
    void Insert(char idx)
    {
    	int ch = idx;
         
         if(arr[ch] == 0)
         {
         	if(begin != -1)
         	{
	         	arr[ch] = -1;
	         	arr[end] = ch;
	         	end = ch;
         	}
         	else
         	{
	         	arr[ch] = -1;
	         	begin = ch;
	         	end = ch;
         	}
         }
         else if(arr[ch] != -2)
         {
     		int pre = -1;
         	for(int i = 0 ; i < 256 ; ++i)
         		if(arr[i] == ch)
         			pre = i;
         	cout<<idx<<" pre : "<<pre<<endl;
         	if(pre == -1)
         	{
         		begin = arr[ch];
         	}
         	else
         	{
         		arr[pre] = arr[ch];
         		if(end == ch)
         			end = pre;
         	}
         	arr[ch] = -2;
         }

    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce()
    {
    	if(begin == -1)
    		return '#';
    	return begin;
    }
private:
	vector<int> arr;
	int begin = -1;
	int end = -1;
};


int main()
{
	Solution s;
	s.Insert('h');
	cout<<s.FirstAppearingOnce()<<",h"<<endl;
	s.Insert('e');
	cout<<s.FirstAppearingOnce()<<",e"<<endl;
	s.Insert('l');
	cout<<s.FirstAppearingOnce()<<",l"<<endl;
	s.Insert('l');
	cout<<s.FirstAppearingOnce()<<",l"<<endl;
	s.Insert('o');
	cout<<s.FirstAppearingOnce()<<",o"<<endl;
	s.Insert('w');
	cout<<s.FirstAppearingOnce()<<",w"<<endl;
	s.Insert('o');
	cout<<s.FirstAppearingOnce()<<",o"<<endl;
	s.Insert('r');
	cout<<s.FirstAppearingOnce()<<",r"<<endl;
	s.Insert('l');
	cout<<s.FirstAppearingOnce()<<",l"<<endl;
	s.Insert('d');
	cout<<s.FirstAppearingOnce()<<",d"<<endl;
	// s.Insert('g');
	// cout<<s.FirstAppearingOnce()<<endl;
	// s.Insert('o');
	// cout<<s.FirstAppearingOnce()<<endl;
	// s.Insert('o');
	// cout<<s.FirstAppearingOnce()<<endl;
	// s.Insert('g');
	// cout<<s.FirstAppearingOnce()<<endl;
	// s.Insert('l');
	// cout<<s.FirstAppearingOnce()<<endl;
	// s.Insert('e');
	// cout<<s.FirstAppearingOnce()<<endl;

	return 0;
}